package newcoder_exp.justPractise;

import java.util.PriorityQueue;

import org.junit.Test;

import utils.BracketUtils;

import java.util.*;

public class NC131 {
    public class Solution {


        /* 
            每行有一个整数opt表示操作类型
            若opt=1，则接下来有一个整数N表示将N加入到结构中。
            若opt=2，则表示询问当前所有数的中位数

            第二点：中位数取较大者
        */
        public double[] flowmedian (int[][] operations) {
            if (operations == null || operations.length == 0) return new double[0];

            Queue<Integer> min = new PriorityQueue<>();
            Queue<Integer> max = new PriorityQueue<>((o1, o2) -> o2 - o1);

            int len = operations.length;
            List<Double> list = new ArrayList<>();

            for (int i = 0; i < len; i++) {
                int[] cur = operations[i];
                if (cur[0] == 1) {
                   if (max.size() == min.size()) {
                        max.offer(cur[1]);
                        min.offer(max.poll());
                   } else {
                        min.offer(cur[1]);
                        max.offer(min.poll());
                   }
                } else {
                    
                    if (min.size() == max.size()) {
                        if (min.size() == 0) {
                            list.add(0.0);
                            continue;
                        }
                        list.add((double)(min.peek() + max.peek()) / 2.0) ;
                    } else {
                        list.add((double)min.peek());
                    }
                }
            }
            return list.stream().mapToDouble(Double::valueOf).toArray();
        }
    }

    @Test
    public void test() {
        Solution s = new Solution();
        int[][] nums = BracketUtils.to2DArray("[[2],[1,333474537],[2],[1,24099757],[1,105424634],[1,506346410],[2],[1,604060694],[1,14769624],[2],[2],[2],[2],[1,529516163],[1,28030889],[2],[2],[1,312258330],[1,975138891],[2],[2],[2],[1,835490373],[2],[1,459647040],[2],[1,595412601],[2],[2],[2],[1,270164978],[2],[2],[2],[1,354900497],[1,371706805],[2],[1,949302551],[2],[2],[2],[2],[1,708179594],[1,816308832],[2],[1,87946100],[1,923142746],[1,357758025],[2],[2],[2],[2],[1,969576074],[1,267704084],[2],[1,16557839],[2],[1,675732426],[1,614995065],[1,71549873],[1,247741440],[1,573200713],[1,914151758],[1,679501423],[2],[1,683155404],[1,574869867],[2],[1,624031519],[1,155168261],[1,910620420],[1,822811135],[2],[2],[2],[1,24562405],[2],[2],[1,452521492],[1,588617679],[1,755732510],[1,309165525],[1,868792732],[1,430746310],[1,142496313],[1,453994020],[2],[2],[2],[1,621204846],[1,184509109],[1,552415112],[1,84710243],[2],[1,275084256],[2],[2],[2],[1,472740326],[2],[1,2603359],[2],[1,415478791],[2],[1,22177372],[2],[1,59819492],[1,497979061],[2],[1,280929338],[2],[1,759217575],[2],[1,996943938],[2],[2],[2],[2],[1,582295480],[1,584611961],[1,12307287],[2],[1,66286192],[2],[2],[1,717324754],[2],[1,90410811],[2],[1,505509311],[1,339699503],[1,274514567],[2],[1,808214445],[1,915508870],[2],[2],[1,404255413],[2],[2],[1,107799778],[1,567326152],[1,20193983],[2],[1,351238479],[2],[1,638563220],[1,900130418],[2],[2],[2],[1,7297010],[2],[2],[1,49954493],[2],[1,47222318],[1,257720412],[1,982319209],[1,831767455],[2],[1,11696725],[1,308783372],[1,360005550],[1,35151069],[2],[2],[2],[2],[1,40678009],[2],[1,609719705],[2],[2],[1,654044625],[2],[2],[2],[1,33991507],[1,48893812],[1,164665325],[1,628224995],[2],[1,583686349],[1,162708958],[2],[1,747708335],[1,273617444],[1,3735806],[2],[1,743476120],[1,909469126],[1,449869533],[2],[1,375779515],[2],[1,112798754],[2],[1,60365554],[2],[2],[1,423255739],[1,236207169],[2],[2],[1,456070709],[2],[1,878021204],[2],[1,582991421],[1,623137678],[1,393116332],[1,249394350],[1,319361921]]");
        double[] res = s.flowmedian(nums);
        System.out.println(Arrays.toString(res));
    }
}
